<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-28
 * Time: 14:29
 * 购物车
 */

namespace app\api\controller\v1;


use app\api\service\goods\CreateOrderContentSer;
use app\lib\exception\ParameterException;
use think\facade\Request;
use app\api\model\GoodsOrderContent as GoodsOrderContentModel;
use app\api\service\Token;

class GoodsOrderContent
{
    /**
     * 加入购物车 或者 直接结算
     */
    public function createOrUpdateData()
    {
        $post = Request::post();
        $createOrderContent = new CreateOrderContentSer($post);
        return $createOrderContent->entrance();
    }

    /**
     * 获取购物车列表
     */
    public function getGoodsOrderContent($goods_ids)
    {
        $uid = Token::getCurrentUid();
        $goods_ids_one = explode("-",$goods_ids);
        if(!$goods_ids) {
            return GoodsOrderContentModel::hasWhere('goods')
                ->with('img')
                ->where('user_id','eq',$uid)
                ->where('order_id','eq',0)
                ->order('id desc')->select()->toArray();
        } else {
            return GoodsOrderContentModel::hasWhere('goods')
                ->with('img')
                ->where('user_id','eq',$uid)
                ->where('order_id','eq',0)
                ->where('goods_id','in',$goods_ids_one)
                ->order('id desc')->select()->toArray();
        }
    }

    /**
     * 删除购物车信息
     */
    public function deleteGoodsOrderContent($id)
    {
        $info = GoodsOrderContentModel::get($id);
        $uid  = Token::getCurrentUid();
        if(!$info) {
            throw new ParameterException([
                'msg' => [
                    'goods' => '需要删除的信息为空'
                ]
            ]);
        } else {
            if($uid != $info['user_id']) {
                throw new ParameterException([
                    'msg' => [
                        'goods' => '不是您的购物车信息'
                    ]
                ]);
            }
            GoodsOrderContentModel::destroy($id);
            return writeJson(203, [], '操作成功！');
        }
    }
}